﻿package com.badibadi.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.badibadi.adapter.UpPictures2Adapter;
import com.badibadi.mytools.MyProgressBar;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ProgressBar;

public class UpPictures2Activity extends BaseActivity {

	public final int UP2_SETADAPTER = 0x02;
	GridView mGridView;
	public android.os.Handler mHandler;
	/**
	 * 从上传图片1处接收过来的路径名
	 */
	String pathName;
	UpPictures2Adapter upPictures2Adapter;
	/**
	 * 存储当前路径下的所有文件
	 */
	String[] files;
	/**
	 * 存放当前路径下所有图片的文件名
	 */
	ArrayList<String> picName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.up_pictures2);
		init();

	}

	private void init() {
		// TODO Auto-generated method stub
		try {
			pathName = getIntent().getBundleExtra(UpPictures1DataKey0)
					.getString(UpPictures1DataKey0);
//			   
		} catch (Exception e) {
			// TODO: handle exception
			ShowToast(UpPictures2Activity.this, "数据错误，无上传图片前流程");
		}

		mGridView = (GridView) this.findViewById(R.id.Up_pictures2_GridView);
		picName = new ArrayList<String>();

		SetGridViewThread sGridViewThread = new SetGridViewThread();
		Thread t = new Thread(sGridViewThread);
		t.start();

		// progressDialog.setTitle(title);

		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				if (msg.what == UP2_SETADAPTER) {

					MyProgressBar myProgressBar = new MyProgressBar();
					myProgressBar.ProgressBarDialog(UpPictures2Activity.this,
							3000);
					InitSetGridViewAdapter();
				}

			}
		};
		mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				// TODO Auto-generated method stub
				ShowToast(getApplicationContext(), "position====你点击了第====L>"
						+ position);
				upPictures2Adapter.ChangeItemClick(getClickView(position),
						position);

			}
		});
	}

	/**
	 * 完成一些费时间的设置picAdress和获取文件名
	 * 
	 * @author Administrator
	 * 
	 */
	public class SetGridViewThread implements Runnable {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			getAllFiles(pathName);
			for (int i = 0; i < files.length; i++) {
				File file = new File(pathName, files[i]);
				if (file.isFile()) {
					String Sfile = pathName + "/" + files[i];
					if (Sfile.lastIndexOf(".jpg") != -1
							|| Sfile.lastIndexOf(".png") != -1
							|| Sfile.lastIndexOf(".jif") != -1) {
						picName.add(files[i]);
					}
				}
			}
			mHandler.sendEmptyMessage(UP2_SETADAPTER);
		}

	}

	/**
	 * 为gridView加载适配器
	 */
	private void InitSetGridViewAdapter() {

		upPictures2Adapter = new UpPictures2Adapter(UpPictures2Activity.this,
				pathName, picName);
		if (mGridView != null)
			mGridView.setAdapter(upPictures2Adapter);
	}

	/**
	 * 获取指定路径下的所有文件和文件夹
	 * 
	 * @param filePath
	 * @return 返回true表示这是一个有效路径，返回false表示这是一个文件
	 */
	public boolean getAllFiles(String filePath) {
		File file = new File(filePath);
		if (file.isDirectory()) {
			files = file.list();

			return true;
		}
		return false;
	}

	/**
	 * （重要：关系到刷新指定位置） 获取当前显示在屏幕上
	 * 
	 * @param position
	 *            需要传入的是点击项（position）
	 * @return 返回的是点击打钩出的View
	 */
	public View getClickView(int position) {
		// 下面的是刷新指定项
		// 获取第一个当前显示的视图
		int visiblePos = mGridView.getFirstVisiblePosition();
//		   
		// 得到需要更新的item得view
		View v = mGridView.getChildAt(position - visiblePos);
//		   
		return v;
	}
}
